package cn.rongdao.jrkabao.ui.remind.uicomponent;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.ui.remind.AddCreditCardRemindActivity;
import cn.rongdao.jrkabao.util.LoginUtil;

/**
 * Created by jack on 2017/11/22
 */

public class RemindDetailsTitleBarUIComponent extends RelativeLayout {

    public RemindDetailsTitleBarUIComponent(Context context) {
        super(context);
    }

    public RemindDetailsTitleBarUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RemindDetailsTitleBarUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvBankName;
    private ImageView ivRefresh;
    private ImageView ivEditor;

    private String remindId;
    private Animation operatingAnim;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivBack = (ImageView) findViewById(R.id.ard_iv_back);
        tvTitle = (TextView) findViewById(R.id.ard_tv_title);
        tvBankName = (TextView) findViewById(R.id.ard_tv_bank_name);
        ivRefresh = (ImageView) findViewById(R.id.ard_iv_refresh);
        ivEditor = (ImageView) findViewById(R.id.ard_iv_editor);

        initAnim();
        initListener();
    }

    private void initAnim() {
        operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_anim);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
    }

    private void initListener() {
        ivEditor.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!LoginUtil.isLogin(getContext())) {
                    return;
                }
                AddCreditCardRemindActivity.start(getContext(), remindId);
            }
        });
    }

    public void initLeftBack() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });
    }

    public void setRemindId(String remindId) {
        if (!FRString.isEmpty(remindId)) {
            this.remindId = remindId;
        } else {
            this.remindId = "-1";
        }
    }

    public void setTitleContent(String title) {
        tvTitle.setText(FRString.valueOf(title));
        tvBankName.setVisibility(View.GONE);
        ivEditor.setVisibility(View.GONE);
    }

    public void setBankName(String bankName, String bankCode) {
        if (FRString.isEmpty(bankCode)){
            tvBankName.setText(FRString.valueOf(bankName));
        }else {
            tvBankName.setText(FRCollection.appendAsString(bankName, "(", bankCode, ")", ""));
        }
        tvTitle.setVisibility(View.GONE);
    }

    public void setOnRefreshClickListener(OnClickListener onClickListener) {
        ivRefresh.setOnClickListener(onClickListener);
    }

    public void startAnim() {
        ((Activity) getContext()).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ivRefresh.startAnimation(operatingAnim);
            }
        });
    }

    public void clearAnim() {
        ((Activity) getContext()).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ivRefresh.clearAnimation();
            }
        });
    }
}